$(function(){
	var nowDate = $(".nowDate"),		//左边的日历盒子
		lstrTd = "",					//左日期的行的DOM结构
		lrows = 0,						//左日期行数
	 	iHtmlNow = "",					//左边的日历结构
		coverHtml="",
	 	nowTitleDateY = "",				//左边标题年份
	  	nowTitleDateM = "",				//左边显示的月份
		nowlastM = "",					//左边的翻月显示
	 	creatbtu = true,				//只创建一次HTML结构的开关
		NumDay = 0,						//左边每个月的天数;
		lfday = 0,						//左边当前月份的第一天，是星期几
		rfday = 0;						//右边当前月份的第一天,是星期几		
	//创建日期行
	function creatTr(l){
		lstrTd="";
		for(i=0;i<l;i++){
			lstrTd += "<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>"
		}
	}
	/*创建当前和下一个月的日期和年份
	*这里分三种情况，当前月为12月 当前月为11月，当前月为1月
	*/
	function getTitleDate(){
		var odate = new Date();
		//如果当前月是12月分，那么右边的月份，就应该是1月份
		if(odate.getMonth()+1 == 12){
			nowTitleDateY = odate.getFullYear();
			nowLastM = nowTitleDateM = odate.getMonth()+1;
		}else if(odate.getMonth()+1 == 11){
			nowTitleDateY = odate.getFullYear();
			nowLastM = nowTitleDateM = odate.getMonth()+1;
		}else if(odate.getMonth()+1 == 1){
			nowTitleDateY = odate.getFullYear();
			nowTitleDateM = odate.getMonth()+1;
			nowLastM = 13;
		}else{
			nowTitleDateY = odate.getFullYear();
			nowLastM = nowTitleDateM = odate.getMonth()+1;
		}
	}
	//获取当前月份的一号，是星期几
	//首先设置你创建日期对象的年份，月份，和你需要知道的日期数，把这些设置好之后，再使用getDay()方法，就可以获取你设置日期的星期数了；
	function getfirstD(m1,y1){
		var odatef = new Date();		
		odatef.setFullYear(y1);
		odatef.setMonth(m1-1)
		odatef.setDate(1);
		lfday = odatef.getDay();	
	}
	//根据大小月份取得天数
	function getTdDay(m1,y1){
		if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12){
			NumDay = 31;
		}else if(m1==4 || m1==6 || m1==9 || m1==11){
			NumDay = 30;
		}else if(m1==2 && isRunYear(y1)){
			NumDay = 29;
		}else if(m1 == 2 ){
			NumDay = 28;
		}
	}
	//根据传入的年份参数，判断是否是润年
	function isRunYear(y){
		if(y%4==0 && y%100 !=0){
			return true;	
		}else if(y%400==0){
			return true;
		}else{
			return false;	
		}
	}	
	//创建HMTL结构
	function creatHtml(creatbtu){
		//根据当前月份的一号是星期几，来生成有几行存放所有日期
		if(lfday>5){
			lrows = 6;
		}else{
			lrows = 5;
		}
		creatTr(lrows);
		iHtmlNow = "<div class='title'>"+nowTitleDateY+"年"+nowTitleDateM+"月</div>"+
				"<table border='0' cellpadding='0' cellspacing='0' class='nowTable'>"+"<thead><tr><th>SU</th><th>MO</th><th>TU</th><th>WE</th><th>TH</th><th>FR</th><th>SA</th></tr></thead>"+"<tbody>"+lstrTd+"</tbody></table>";
		if(lrows==5){
			coverHtml="<table border='0' id='coverTable' style='margin:-182px auto 0 auto;'>";
		}else{
			coverHtml="<table border='0' id='coverTable' style='margin:-212px auto 0 auto;'>";
		}
		coverHtml+="<thead><tr><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr></thead><tbody>"+lstrTd+"</tbody></table>";
	}
	//将日期插入到对应的TD当中
	function insertdate(d){
		for(i=0;i<NumDay;i++){
			$(".nowDate table tbody td").eq(i+d).text(i+1)
		}	
	}
	function insertHtml(){
		nowDate.append(iHtmlNow);
		nowDate.append(coverHtml);
	}	
	getTitleDate();
	getTdDay(nowTitleDateM,nowTitleDateY);
	getfirstD(nowTitleDateM,nowTitleDateY);
	creatHtml();	
	insertHtml();			
	insertdate(lfday);
	nowDate.slideDown(200);
	
});
jQuery(document).ready(function($){
	 var url = window.location.toString(); 
	 var id = url.split("#")[1]; 
	 if(id){ 
		var t = $("#"+id).offset().top; 
		$(window).scrollTop(t); 
	}
	//弹窗
	$('.user_div').on('click', function(event){
		if($(event.target).is('.sign_button')||$(event.target).is('.rule_a')){
			event.preventDefault();
			$.ajax({
				type:"post",
				url:_appRoot+"/common/querySignList",
				dataType:"json",
				success: function(data){
					if(data.message!=0){
						var userProperty=parseInt($(".userProperty").text());
						for(var i=0;i<data.length;i++){
							var date=parseInt(data[i].signDate.substring(8,10));
							signThisDay(date);
							var d=new Date();
							var today=d.getDate();
							if(date==today){
								$(".sign_a").text("今日已签").addClass("signed").removeAttr("href");
								$(".sign_tip").html("签到成功，10积分已计入您的账户<br/>您有"+userProperty+"积分，可抵扣"+userProperty/100+"元").addClass("signed_tip");
								$("")
							}
						}
					}
					else{
						clickautohide(data.message,3);
					}
				}
			});
			$('#cd-popup-mall').fadeIn(300);
			$('#cd-popup-mall #signDiv').fadeIn(300);
		}
	});
	$('#cd-popup-mall').on('click', function(event){
		if($(event.target).is('#cd-popup-mall')||$(event.target).is('.close_popup')||$(event.target).is('.close_exchange')||$(event.target).is('.cancel_but')){
			event.preventDefault();
			$(this).fadeOut(300);
			$('.cd-popup-container').fadeOut(300);
		}
	});

	$(document).keyup(function(event){
    	if(event.which=='27'){
			$('#cd-popup-mall').fadeOut(300);
			$('.cd-popup-container').fadeOut(300);
	    }
    });
	//弹出兑换实物框
	$('.exchange_a').on('click', function(event){
		event.preventDefault();
		var itemDiv=$(this).closest(".item_div");
		$(".product_image1").attr("src",itemDiv.find(".item_image").attr("src"));
		$(".product_name1").text(itemDiv.find(".item_title").text());
		$("#price1").val(itemDiv.find(".after_price").text());
		$("#price1").attr("data",itemDiv.attr("data"));
		$(".price_font1").text($("#price1").val());
		$(".product_num1").text(1);
		$('#cd-popup-mall').fadeIn(300);
		$('#cd-popup-mall #exchangeDiv').fadeIn(300);
	});
	$(".course_ul li").click(function(){
		var goodsType=$(this).attr("data");
		$("#goodsType").val(goodsType);
		$(".pageForm").submit();
	});
	//弹出兑换课程框
	$(".changeCourse").on('click',function(event){
		event.preventDefault();
		var courseLi=$(this).closest("li");
		$(".product_image2").attr("src",courseLi.find(".course_image").attr("src"));
		$(".product_name2").text(courseLi.find(".span1").text());
		$("#price2").val(courseLi.find(".after_price2").text());
		$("#price2").attr("data",courseLi.attr("data"));
		$(".price_font2").text($("#price2").val());
		$(".product_num2").text(1);
		$('#cd-popup-mall').fadeIn(300);
		$('#cd-popup-mall #changeCourseDiv').fadeIn(300);
	});
	//弹出游戏框
	$(".game_a").on('click',function(event){
		event.preventDefault();
		$("#game_iframe").attr("src",_appRoot+"/common/jump.html");
		$('#cd-popup-mall').fadeIn(300);
		$('#cd-popup-mall #game_Div').fadeIn(300);
	});
});
//今日签到
function signToday(){
	if($("#userIsLogin").val()==0){
		clickautohide("请先登录！",1);
		return;
	}
	$.ajax({
		type:"post",
		url:_appRoot+"/common/dailySign",
		dataType:"json",
		success: function(data){
			if(data.result==1){
				var userProperty=parseInt($(".userProperty").text())+10;
				$(".sign_a").text("今日已签").addClass("signed").removeAttr("href");
				$(".sign_tip").html("签到成功，10积分已计入您的账户<br/>您有"+userProperty+"积分，可抵扣"+userProperty/100+"元").addClass("signed_tip");
				$(".integral").text(userProperty);
				$(".myInteger").text(userProperty);
				var d=new Date();
				var today=d.getDate();
				signThisDay(today);
			}
			else{
				clickautohide(data.message,3);
			}
		}
	});
}
function changeNum(a,n){
	var sum=parseInt($(".product_num"+n).text());
	var price=$("#price"+n).val();
	if((sum<=1&&a==-1)||(sum>=99&&a==1)){
		return false;
	}
	$(".product_num"+n).text(sum+a);
	$(".price_font"+n).text((sum+a)*price);
}
//记录本月签到日
function signThisDay(day){
	$(".nowTable td").each(function(){
		if($(this).text()==day){
			var index=$(".nowTable td").index($(this)[0]);
			$("#coverTable td:eq("+index+")").addClass("signDay");
		}
	});
}
//兑换实物
function confirmExchange(){
	if($("#userIsLogin").val()==0){
		clickautohide("请先登录！",1);
		return;
	}
	var propId=$("#price1").attr("data");
	var sum=$(".product_num1").text();
	var	receiverName=$("#buyer_name").val();
	if(receiverName==""){
		clickautohide("收件人姓名不能为空！",3);
		return;
	}
	var receiverAddress=$("#buyer_adress").val();
	if(receiverAddress==""){
		clickautohide("收件人地址不能为空！",3);
		return;
	}
	var receiverPhone=$("#buyer_phone").val();
	if(receiverPhone==""){
		clickautohide("收件人联系电话不能为空！",3);
		return;
	}
	$.ajax({
		type:"POST",
		url:_appRoot+"/common/exchangeProp",
		data:{propId:propId,sum:sum,receiverName:receiverName,receiverAddress:receiverAddress,receiverPhone:receiverPhone},
		dataType:"json",
		success:function(data){
			if(data.result==1){
				clickautohide("兑换成功!",2);
				var nowInteger=parseInt($(".userProperty").text())-parseInt($(".price_font1").text());
				$(".integral").text(nowInteger);
				$(".myInteger").text(nowInteger);
				$('#cd-popup-mall').fadeOut(300);
				$('#cd-popup-mall #exchangeDiv').fadeOut(300);
			}else{
				clickautohide(data.message,1);
			}
		}
		
	});
}
//兑换课程
function exhangeCourse(){
	if($("#userIsLogin").val()==0){
		clickautohide("请先登录！",1);
		return;
	}
	var priceId=$("#price2").attr("data");
	var goodsType=$("http://www.yxit.net/js/common/.course_left .on").attr("data");
	$.ajax({
		type:"POST",
		url:_appRoot+"/common/exchangeGoods",
		data:{priceId:priceId,goodsType:goodsType},
		dataType:"json",
		success:function(data){
			if(data.result==1){
				clickautohide("兑换成功!",2);
				var nowInteger=parseInt($(".userProperty").text())-parseInt($(".price_font2").text());
				$(".integral").text(nowInteger);
				$(".myInteger").text(nowInteger);
				$('#cd-popup-mall').fadeOut(300);
				$('#cd-popup-mall #changeCourseDiv').fadeOut(300);
				window.location.href=_appRoot+"/userCenter/studentOrderDetail?id="+data.message;
			}else{
				clickautohide(data.message,1);
			}
		}
		
	});
}